package cc.sjfx.sharedoc.function;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.http.util.EncodingUtils;

import cn.mandroid.frame.tools.MD5;
import android.content.Context;

public class ReadAndWriteFile {
	Context context;
	public ReadAndWriteFile(Context context){
		this.context=context;
	}
	public String readFile(String fileName)  {
		String res = "";
		try {
			FileInputStream fin = context.openFileInput(MD5.getMd5Code(fileName));
			int length = fin.available();
			byte[] buffer = new byte[length];
			fin.read(buffer);
			res = EncodingUtils.getString(buffer, "UTF-8");
			fin.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}
	public void writeFile(String fileName,String writestr)  {
		try {
			FileOutputStream fout = context.openFileOutput(MD5.getMd5Code(fileName), Context.MODE_PRIVATE);
			byte[] bytes = writestr.getBytes();
			fout.write(bytes);
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
